// OptionsSaveHandler.cpp: implementation of the OptionsSaveHandler class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "kwservercore.h"
#include "OptionsSaveHandler.h"
#include "UserPreferenceSet.h"
#include "DatabaseManager.h"

#include "CommonLib\Packet.h"
#include "CommServerLib\CommWithClient.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

OptionsSaveHandler::OptionsSaveHandler(const Packet& packet, HANDLE hClient):
	BaseRequestHandler(packet, hClient)
{
}

OptionsSaveHandler::~OptionsSaveHandler()
{
}

int OptionsSaveHandler::Handling()
{
	OptionsSaveStruct * pOptionsSaveStruct = (OptionsSaveStruct *)m_pPacket->GetpData();

	bool bSuccess = SaveOptions(pOptionsSaveStruct);
	pOptionsSaveStruct->m_nMethodAck = bSuccess?VCS_MR_OK:VCS_MR_FAILTOSAVEOPTIONS;

	Packet packetAck(sizeof(VizClientServer), (LPBYTE)pOptionsSaveStruct);
	packetAck.m_dwId = m_pPacket->m_dwId;
	CommWithClient::Instance()->SendTo(packetAck, m_hClient);

	return 0;
}

bool OptionsSaveHandler::SaveOptions(OptionsSaveStruct * pOptionsSaveStruct)
{
	bool bRet = false;

	CDatabase * pDatabase = DatabaseManager::Instance()->GetpDatabase();
	CUserPreferenceSet UserPreferenceSet(pDatabase);

	CString strSql;
	strSql.Format("DELETE FROM UserPreference WHERE UserId = '%s'", pOptionsSaveStruct->m_szUserId);
	pDatabase->ExecuteSQL(strSql);

	UserPreferenceSet.m_strFilter.Format("UserId = '%s'", pOptionsSaveStruct->m_szUserId);
	if (UserPreferenceSet.Open())
	{
		ASSERT(UserPreferenceSet.IsEOF());
		for (int i = 1;i <= OPTIONNUM;i++)
		{
			UserPreferenceSet.AddNew();
			UserPreferenceSet.m_UserId = pOptionsSaveStruct->m_szUserId;
			UserPreferenceSet.m_OptionId = i;
			switch (i)
			{
			case 1:
				UserPreferenceSet.m_Value = pOptionsSaveStruct->m_Options.m_szHomePageURL;
				break;
			case 2:
				{
					CString strColor;
					CString strTemp;
					strColor.Format("%d", pOptionsSaveStruct->m_Options.m_crStateColors[0]);
					for (int i=1;i<10;i++)
					{
						strTemp.Format(",%d", pOptionsSaveStruct->m_Options.m_crStateColors[i]);
						strColor = strColor + strTemp;
					}
					UserPreferenceSet.m_Value = strColor;
					break;
				}
			case 3:
				UserPreferenceSet.m_Value.Format("%d", pOptionsSaveStruct->m_Options.m_dwAlarmStateMask);
				break;
			case 4:
				UserPreferenceSet.m_Value.Format("%d",pOptionsSaveStruct->m_Options.m_dwInversionMask);
				break;
			case 5:
				UserPreferenceSet.m_Value.Format("%d",pOptionsSaveStruct->m_Options.m_nRTCurvePenWidth);
				break;
			case 6:
				UserPreferenceSet.m_Value.Format("%d",pOptionsSaveStruct->m_Options.m_nRTCurveTime);
				break;
			case 7:
				UserPreferenceSet.m_Value.Format("%d",pOptionsSaveStruct->m_Options.m_nRTCurveRefreshCycle);
				break;
			case 8:
				UserPreferenceSet.m_Value.Format("%d",pOptionsSaveStruct->m_Options.m_nHSCurveTime);
				break;
			case 9:
				UserPreferenceSet.m_Value.Format("%d",pOptionsSaveStruct->m_Options.m_nHSCurveSamplingTime);
				break;
			case 10:
				UserPreferenceSet.m_Value.Format("%d",pOptionsSaveStruct->m_Options.m_nArrangeStyle);
				break;
			case 11:
				UserPreferenceSet.m_Value.Format("%d",pOptionsSaveStruct->m_Options.m_nNumOfColumns);
				break;
			case 12:
				UserPreferenceSet.m_Value.Format("%d",pOptionsSaveStruct->m_Options.m_dwDisplayContentMask);
				break;
			case 13:
				UserPreferenceSet.m_Value.Format("%d", pOptionsSaveStruct->m_Options.m_nHSCurveAspectRatio);
				break;
			case 14:
				UserPreferenceSet.m_Value.Format("%d", pOptionsSaveStruct->m_Options.m_bHSCurvePrintInfo);
				break;
			case 15:
				UserPreferenceSet.m_Value.Format("%d", pOptionsSaveStruct->m_Options.m_dwHSCurvePrintInfoMask);
				break;
			case 16:
				UserPreferenceSet.m_Value.Format("%d", pOptionsSaveStruct->m_Options.m_nSortBy);
				break;
			case 17:
				UserPreferenceSet.m_Value.Format("%d", pOptionsSaveStruct->m_Options.m_nAlarmMode);
				break;
			default:
				break;
			}
			UserPreferenceSet.Update();
		}
		UserPreferenceSet.Close();
		bRet = true;
	}

	return bRet;
}

bool OptionsSaveHandler::Prehandling()
{
	return true;
}